using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Grr
{
	public static class Validator
	{
		private static Regex email_regex = new Regex (@"^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$");

		public static readonly int MinUserNameLength = 1;
		public static readonly int MaxUserNameLength = 36;

		public static readonly int MinEmailLength = 5;
		public static readonly int MaxEmailLength = 100;

		public static readonly int MinPasswordLength = 1;
		public static readonly int MaxPasswordLength = 20;

		public static readonly int MinLocationLength = 0;
		public static readonly int MaxLocationLength = 36;

		public static readonly int MinGrurrahNameLength = 5;
		public static readonly int MaxGrurrahNameLength = 60;

		public static readonly int MinTagNameLength = 1;
		public static readonly int MaxTagNameLength = 50;

		public static readonly int MinFeatureNameLength = 1;
		public static readonly int MaxFeatureNameLength = 60;

		public static bool IsValidUserName(string username)
		{
			return IsValidCleanRangedString(username, MinUserNameLength, MaxUserNameLength);
		}
		
		public static bool IsValidEmail(string email)
		{
			if (email == null || email.Length < MinEmailLength || email.Length > MaxEmailLength)
				return false;

			return email_regex.IsMatch(email);
		}

		public static bool IsValidPassword(string pass)
		{
			if (pass == null || pass == String.Empty || pass.Length < MinPasswordLength || pass.Length > MaxPasswordLength)
				return false;
			return true;
		}

		public static bool IsValidLocation(string location)
		{
			return IsValidCleanRangedString(location, MinLocationLength, MaxLocationLength);
		}

		public static bool IsValidGrurrahName(string name)
		{
			return IsValidCleanRangedString(name, MinGrurrahNameLength, MaxGrurrahNameLength);
		}

		public static bool IsValidTagName(string tag)
		{
			return IsValidCleanNonPuncRangedString(tag, MinTagNameLength, MaxTagNameLength);
		}

		public static bool IsValidFeatureName(string name)
		{
			return IsValidCleanNonPuncRangedString(name, MinFeatureNameLength, MaxFeatureNameLength);
		}

		public static bool IsValidCleanRangedString(string str, int min_length, int max_length)
		{
			if (str == null || str == String.Empty || str.Length < min_length || str.Length > max_length)
				return false;
			string clean_str = StringUtil.Clean(str);
			return clean_str == str;
		}

		public static bool IsValidCleanNonPuncRangedString(string str, int min_length, int max_length)
		{
			if (str == null || str == String.Empty || str.Length < min_length || str.Length > max_length)
				return false;
			string clean_str = StringUtil.CleanNoPunc(str);
			return clean_str == str;
		}
	}
}

